package practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 算法练习：第十一天
 */
class Reader {
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
}
public class Day11 {
    /**
     * 分组（模拟+优化：二分查找）
     */
    public static void main1(String[] args) throws IOException {
        Reader in = new Reader();
        int n = in.nextInt();
        int m = in.nextInt();
        Map<Integer,Integer> map = new HashMap<>();
        int max = 0;
        for(int i = 0; i < n; i++) {
            int a = in.nextInt();
            map.put(a,map.getOrDefault(a,0) + 1);
            max = Math.max(max,map.get(a));
        }
        int ret = Integer.MAX_VALUE;
        for(int i = 1; i <= max; i++) {
            int j = 0;
            for(Map.Entry entry : map.entrySet()) {
                int value = (int)entry.getValue();
                int a = 0;
                if(value % i != 0) {
                    a = 1;
                }
                j += (value / i) + a;
            }
            if(j <= m) {
                ret = Math.min(ret,i);
                break;
            }
        }
        if(ret == Integer.MAX_VALUE) {
            System.out.println(-1);
        }else {
            System.out.println(ret);
        }
    }

    /**
     * 拓扑排序（建图和统计入度为0的点）
     */
    public static void main2(String[] args) throws IOException {
        Reader in = new Reader();
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] arr = new int[m][2];
        int[] count = new int[n + 1];
        Map<Integer, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < m; i++) {
            arr[i][0] = in.nextInt();
            arr[i][1] = in.nextInt();
//            if (map.get(arr[i][0]) == null) {
//                map.put(arr[i][0], new ArrayList<>());
//            }
            map.computeIfAbsent(arr[i][0],t -> new ArrayList<>());
            map.get(arr[i][0]).add(arr[i][1]);
            count[arr[i][1]]++;
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            if (count[i] == 0) {
                q.offer(i);
            }
        }

        int[] ret = new int[n];
        int k = 0;
        while (!q.isEmpty()) {
            int top = q.poll();
            ret[k++] = top;
            for (int x : map.getOrDefault(top, new ArrayList<>())) {
                count[x]--;
                if (count[x] == 0) {
                    q.offer(x);
                }
            }
        }
        if (k != n) {
            System.out.println(-1);
            return;
        }
        for (int i = 0; i < n; i++) {
            if (i == n - 1) {
                System.out.print(ret[i]);
            } else {
                System.out.print(ret[i] + " ");
            }
        }
    }

}
